package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.OrganizaModel;

import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/3/29.
 */

public class TeamCircleCreateCompanySelectDialog extends Dialog implements RecycleVAdapter.OnItemClickListener {

    private List<CompanyModel> companyModels;
    private CompanyAdapter companyAdapter;
    private RecyclerView contentRecyclerView;

    public TeamCircleCreateCompanySelectDialog(Context context) {
        super(context, R.style.custom_idalgo);

        View contentView = View.inflate(context, R.layout.dialog_team_circle_company_select, null);
        ((TextView) contentView.findViewById(R.id.company_selected_title)).setText("选择要发表的组织");
        contentRecyclerView = (RecyclerView) contentView.findViewById(R.id.company_selected_rv);
        contentRecyclerView.setHasFixedSize(true);
        contentRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));

        companyModels = new ArrayList<>();
        companyAdapter = new CompanyAdapter(getContext(), new ArrayList<CompanyModel>());
        companyAdapter.addItemClickListener(this);
        contentRecyclerView.setAdapter(companyAdapter);

        setContentView(contentView);
        getWindow().getAttributes().height = (int)(DensityUtil.getScreenHeight() * 0.6);
    }

    public void show(List<OrganizaModel> companyModels){
        contentRecyclerView.scrollToPosition(0);
        this.companyModels.clear();
        if(companyModels != null && companyModels.size() > 0) {
            for (int i = 0; i < companyModels.size(); i++) {
                OrganizaModel organizaModel = companyModels.get(i);
                if(organizaModel.getCompanyState() == 1
                        && (organizaModel.getAuditStete() == 0 || organizaModel.getAuditStete() == 2))
                    continue;
                CompanyModel companyModel = new CompanyModel();
                companyModel.setCompanyName(organizaModel.getCompanyName());
                companyModel.setCompanyId(organizaModel.getCompanyId());
                companyModel.setCompanyType(organizaModel.getCompanyType());
                companyModel.setPosition(i);
                this.companyModels.add(companyModel);
            }
        }
        companyAdapter.updateAdapter(this.companyModels);
        super.show();
    }

    @Override
    public void itemSelect(int position) {
        if(this.companySelectedListener != null) {
            CompanyModel companyModel = companyModels.get(position);
            this.companySelectedListener.onCompanySelected(companyModel.getCompanyId(),
                    companyModel.getCompanyName(), companyModel.getCompanyType());
        }
        dismiss();
    }

    private class CompanyAdapter extends RecycleVAdapter<CompanyModel>{
        public CompanyAdapter(Context context, @NonNull List<CompanyModel> list) {
            super(context, list);
        }
        @Override
        public int getLayoutId(int viewType) {
            return R.layout.dialog_team_circle_company_item;
        }
        @Override
        public void onBind(RecycleVHolder holder, int position) {
            holder.getConvertView().setBackgroundResource(R.drawable.normal_click_bg);
            holder.setTextView(R.id.company_name, getData(position).getCompanyName());
            ImageView rightIcon = holder.getImageView(R.id.unselect_company);
            rightIcon.setImageResource(R.mipmap.faxiangengduo);
            rightIcon.setClickable(false);
            rightIcon.setEnabled(false);
        }
    }

    private class CompanyModel{
        private String companyName;
        private String companyId;
        private int companyType;
        private int position;
        public String getCompanyName() {
            return companyName;
        }
        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }
        public String getCompanyId() {
            return companyId;
        }
        public void setCompanyId(String companyId) {
            this.companyId = companyId;
        }
        public int getCompanyType() {
            return companyType;
        }
        public void setCompanyType(int companyType) {
            this.companyType = companyType;
        }
        public int getPosition() {
            return position;
        }
        public void setPosition(int position) {
            this.position = position;
        }
    }

    private CompanySelectedListener companySelectedListener;
    public void setCompanySelectedListener(CompanySelectedListener companySelectedListener){
        this.companySelectedListener = companySelectedListener;
    }
    public interface CompanySelectedListener{
        void onCompanySelected(String companyId, String companyName, int companyType);
    }
}
